package com.hanscan.persistence.controllers;

import com.hanscan.persistence.EntityManagerSingleton;
import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.Company;
import com.hanscan.persistence.entities.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;


public class CompanyJpaController implements Serializable
{

    public CompanyJpaController()
    {
    }

    public EntityManager getEntityManager()
    {
        return EntityManagerSingleton.getInstance();
    }

    public boolean create(Company company) 
    {
           EntityManager em = null;
           try
           {
                em = getEntityManager();
                em.getTransaction().begin();
                em.persist(company);
                em.getTransaction().commit();
                return true;
           }
           catch (Exception ex)
           {
               ex.printStackTrace();
               return false;
           }        
    }

    public boolean edit(Company company)
    {
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            company = em.merge(company);
            em.getTransaction().commit();
            return true;
        }
        catch (Exception ex)
        {
           ex.printStackTrace();
           return false;
        }
    }

    public int destroy(Company company)
    {
        try
        {
            EntityManager em = null;
            UserJpaController userJpaController = new UserJpaController();
            List<User> list = new ArrayList<User>();
            list = userJpaController.findCompany(company.getCompanyId());

            if(list.size()== 0)
            {
                Calendar c = Calendar.getInstance();
                Date date = new Date();
                date = c.getTime();
                em = getEntityManager();
                em.getTransaction().begin();
                company.setResigntDate(date);
                company = em.merge(company);
                em.getTransaction().commit();
                return 0;
            }
            else
            {
                return -1;
            }
        }
        catch (NonexistentEntityException ex)
        {
            return -2;
        }
    }

    public List<Company> getAllCompaniesOrderByName() throws NonexistentEntityException
    {
        EntityManager em = getEntityManager();
        em.clear();
        try
        {
            Query query = em.createNamedQuery("getAllCompaniesOrderByName");
            List<Company> list = (List<Company>) query.getResultList();
            return list;
        }
        catch (NoResultException ex)
        {
            ex.printStackTrace();
            throw new NonexistentEntityException(ex.getMessage());
        }
    }

    public List<Company> findCompanyByName(String name) throws NonexistentEntityException
    {
        EntityManager em = getEntityManager();
        em.clear();
        try
        {
            Query query = em.createQuery("select c from Company as c where c.companyName LIKE '%" + name + "%' and c.resigntDate = null");
//            Query query = em.createNamedQuery("findCompanyByName");
//            query.setParameter(1, name);
            List list =  query.getResultList();
            return list;
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            throw new NonexistentEntityException(ex.getMessage());
        }
    }

    public Company findCompanyByCompanyId(long id) throws NonexistentEntityException
    {
        EntityManager em = getEntityManager();
        em.clear();
        try
        {
            Query query = em.createNamedQuery("findCompanyByCompanyId");
            query.setParameter(1, id);
            Company company =  (Company) query.getSingleResult();
            return company;
        }
        catch (NoResultException ex)
        {
            ex.printStackTrace();
            throw new NonexistentEntityException(ex.getMessage());
        }
    }

}
